package com.changgou.business.service.impl;


import com.changgou.business.dao.AdMapper;
import com.changgou.business.pojo.AConfig;
import com.changgou.business.service.AdService;
import com.changgou.business.pojo.Ad;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class AdServiceImpl implements AdService {


    @Autowired
    private AdMapper adMapper;

    @Override
    public Ad findById(int id) {
        return adMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Ad> findAll() {
        return null;
    }

    //设置置顶
    @Override
    public void updateTop(int id, int position) {
        /*adMapper.updateTop(id,position);*/
        //根据position查询出当前广告位置置顶状态为1的项，修改为0
        adMapper.updateTopToZero(position);
        //再将当前的id对应的广告置顶状态修改为1
        adMapper.updateToTop(id);
    }

    /*
    添加广告
     */
    @Override
    public void add(Ad ad) {
        adMapper.insertSelective(ad);
    }

    /**
     * 广告分页条件查询
     * @param searchMap
     * @param page 当前页
     * @param size  每页显示的条数
     * @return
     */
    @Override
    public Page<Ad> findPage(Map<String,Object> searchMap,int page, int size) {

        //设置分页
        PageHelper.startPage(page,size);


        //设置当前的查询条件
        Example example = new Example(Ad.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null){
            //设置品牌名称模糊查询
            if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))){
                criteria.andLike("name","%"+searchMap.get("name")+"%");
            }

            if (searchMap.get("position") != null){
                criteria.andEqualTo("position",searchMap.get("position"));
            }

            if (searchMap.get("deadline") != null){
                criteria.andEqualTo("deadline",searchMap.get("deadline"));
            }
        }
        Page<Ad> pageInfo = (Page<Ad>) adMapper.selectByExample(example);
        return pageInfo;
    }

    /**
     * 广告删除
     * @param id
     */
    public void delete(Integer id) {
        adMapper.deleteByPrimaryKey(id);
    }
}
